<?php namespace Common\Hook;

class AppInitHook
{
    //运行钓子
    public function run(&$options)
    {
        //设置站长
        C('rbac.super_user',C('web.WEB_MASTER'));

        //检测安装
        if (!file_exists(APP_PATH . '/Install/Lock.php'))
        {
            if (MODULE != 'Install')
            {
                go(__ROOT__.'/index.php?m=Install&c=Index&a=index');
            }
        }
        //加载插件
        $this->loadAddons();
    }
    
    //加载系统插件
    private function loadAddons()
    {
        $data = S('hooks');
        if (!$data || DEBUG) {
            $hooks = Db::table('hooks')->where('status',1)->lists('name,addons');
            
            if ($hooks) {
                foreach ($hooks as $key => $value) {
                    if ($value) {
                        $names = explode(',', $value);
                        $data = Db::table('addons')->where('status',1)
                        ->whereIn('name',$names)->lists('id,name');
                        if ($data) {
                            $addons = array_intersect($names, $data);
                            Hook::add($key, $addons);
                        }
                    }
                }
            }
            S('hooks', Hook::get());
        } else {
            Hook::import($data, false);
        }
    }
}